package com.gaga.console.core.controller.sys;


import com.gaga.common.enums.DelFlag;
import com.gaga.common.path.ApiPath;
import com.gaga.common.result.Result;
import com.gaga.common.tree.ForestNodeMerger;
import com.gaga.console.base.controller.BaseController;
import com.gaga.console.base.entity.PageParams;
import com.gaga.console.core.entity.SysMenu;
import com.gaga.console.core.service.SysMenuService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ArrayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 菜单权限表 前端控制器
 *
 * @author liujia
 * @since 2022-07-14
 */
@Api(tags = "系统模块-系统菜单")
@RestController
@RequestMapping(ApiPath.ROOT + "/sys/menu")
public class SysMenuController extends BaseController {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 菜单树
     *
     * @return Result
     */
    @GetMapping("/tree")
    @ApiOperation(value = "菜单树", notes = "根据roleId查询菜单树")
    public Result tree() {

        return Result.ok(sysMenuService.routes());
    }

    /**
     * 获取列表
     */
    @ApiOperation(value = "查询list")
    @PostMapping("/list")
    public Result list(@RequestBody PageParams<SysMenu> data) {
        return Result.ok(ForestNodeMerger.merge(sysMenuService.searchList(data)));
    }

    /**
     * 根据详细信息
     */
    @ApiOperation(value = "获取单个记录")
    @GetMapping(value = "/{id}")
    public Result getEntity(@PathVariable Long id) {
        return Result.ok(sysMenuService.getById(id));
    }


    /**
     * 保存
     */
    @ApiOperation(value = "保存记录")
    @PostMapping("/save")
    public Result save(@Validated @RequestBody SysMenu entity) {
        return toAjaxBoolean(sysMenuService.saveOrUpdate(entity));
    }


    /**
     * 菜单是否包含子菜单
     *
     * @param sysMenu 菜单对象
     * @return boolean
     */
    @GetMapping("/check-child")
    @ApiOperation(value = "菜单是否包含子菜单")
    public Result checkChild(SysMenu sysMenu) {

        return Result.ok(sysMenuService.checkChild(sysMenu.getId()));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除记录")
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {

        if (ArrayUtil.isEmpty(ids)) {
            return toAjaxBoolean(false);
        }

        List<SysMenu> removeEntityList = Arrays.stream(ids)
                .map(id -> {
                    SysMenu entity = new SysMenu().setId(id);
                    entity.setDelFlag(DelFlag.DELETED.getCode());
                    entity.setDeleteTime(new Date());
                    return entity;
                }).collect(Collectors.toList());

        return toAjaxBoolean(sysMenuService.updateBatchById(removeEntityList));
    }


}
